package com.voxel.sense.infomsg.module.task.send.userlist;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.com.common.app.BaseActivity;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.ConditionBean;
import com.voxel.sense.infomsg.view.decoration.ItemDivider;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cyy.example.com.facotry.constants.IntentConstants;

public class UserSeletionListActivity extends BaseActivity {

    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    @BindView(R.id.et_search)
    EditText et_search;
    @BindView(R.id.bt_search)
    Button bt_search;
    private UserPickRecyclerAdapter mAdapter;

    public static void show(Activity context, ArrayList list) {
        Intent intent = new Intent(context, UserSeletionListActivity.class);
        intent.putParcelableArrayListExtra(IntentConstants.LIST_TYPE, list);
        context.startActivityForResult(intent, IntentConstants.USER_SELECT_LIST);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_user_seletion_list;
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        ArrayList<Parcelable> datas = getIntent().getParcelableArrayListExtra(IntentConstants.LIST_TYPE);
        return datas != null && datas.size() > 0;
    }

    @Override
    protected void initData() {
        super.initData();
        ArrayList<ConditionBean> datas = getIntent().getParcelableArrayListExtra(IntentConstants.LIST_TYPE);
        ArrayList<ConditionBean> selectedDatas = new ArrayList<>();
        for (ConditionBean data : datas) {
            if (data.isSelect()) {
                selectedDatas.add(data);
            }
        }
        mAdapter = new UserPickRecyclerAdapter(datas);
        mAdapter.setSeletedDatas(selectedDatas);
        recycle_view.setLayoutManager(new LinearLayoutManager(this));
        recycle_view.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider));
        recycle_view.setAdapter(mAdapter);
        bt_search.setText("确定");
    }



    @OnClick({R.id.btn_search_left, R.id.bt_search})
    void OnClickEvent(View view) {
        onBackPressed();
    }

    @OnTextChanged(R.id.et_search)
    void onTextChangedEvent(CharSequence text, int start, int before, int count) {
        //将筛选条件传入
        mAdapter.getFilter().filter(text);
    }


    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(IntentConstants.LIST_TYPE, (ArrayList<? extends Parcelable>) mAdapter.getSourceList());
        intent.putParcelableArrayListExtra(IntentConstants.EXTRAX_DATA, (ArrayList<? extends Parcelable>) mAdapter.getSeletedDatas());
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }
}
